﻿using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace OakEngine.DebugTools
{
    public class OAK_DrawLine
    {
        Texture2D m_t2dBlankTexture;

        Vector2 m_vecStartPoint;
        Vector2 m_vecEndPoint;

        int m_nLineThickness;
        public int LineThickness { get { return m_nLineThickness; } set { m_nLineThickness = value; } }

        float m_fAngle;
        float m_fLineLength;

        /// <summary>
        /// Set up a 2D line
        /// </summary>
        /// <param name="device"> The Graphic Device</param>
        /// <param name="_nWidth">Line WIdth</param>
        /// <param name="_nHeight">Line Height</param>
        /// <param name="data">Color Data to set up just (new [] {Color.white})</param>
        public OAK_DrawLine(GraphicsDevice device, Color[] data = null)
        {
            // Create a 1x1 texture
            this.m_t2dBlankTexture = new Texture2D(device, 1, 1, false, SurfaceFormat.Color);

            if (null == data)
                m_t2dBlankTexture.SetData(new[] { Color.White });
            else
                m_t2dBlankTexture.SetData(data);

            m_nLineThickness = 2;

            m_fAngle = m_fLineLength = 0.0f;

        }

        /// <summary>
        /// Set up a 2D line
        /// </summary>
        /// <param name="device"> The Graphic Device</param>
        /// <param name="_nWidth">Line WIdth</param>
        /// <param name="_nHeight">Line Height</param>
        /// <param name="data">Color Data to set up just (new [] {Color.white})</param>
        public OAK_DrawLine(ref Texture2D texture, Color[] data = null)
        {
            // Create a 1x1 texture
            this.m_t2dBlankTexture = texture;

            if (null == data)
                m_t2dBlankTexture.SetData(new[] { Color.White });
            else
                m_t2dBlankTexture.SetData(data);

            m_nLineThickness = 2;

            m_fAngle = m_fLineLength = 0.0f;

        }

        public void ChangeLineColor(Color[] data = null)
        {
            m_t2dBlankTexture.SetData(data);

        }


        public Vector2 SetStartPoint { set { m_vecStartPoint = value; } }
        public Vector2 SetEndPoint { set { m_vecEndPoint = value; } }

        public void UpdateLinePosition(Vector2 point1, Vector2 point2)
        {
            m_vecStartPoint = point1;
            m_vecEndPoint = point2;
        }

        public void DrawLine(SpriteBatch batch)
        {
            m_fAngle = (float)Math.Atan2(m_vecEndPoint.Y - m_vecStartPoint.Y, m_vecEndPoint.X - m_vecStartPoint.X);
            m_fLineLength = Vector2.Distance(m_vecStartPoint, m_vecEndPoint);


            batch.Draw(m_t2dBlankTexture,          // Texture
                       m_vecStartPoint,
                       null,
                       Color.White,
                       m_fAngle,
                       Vector2.Zero,
                       new Vector2(m_fLineLength, m_nLineThickness),
                       SpriteEffects.None, 0);

        }

    }
}
